/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package com.bullionxpress.client.application;
import java.rmi.*;
import java.rmi.registry.*;
import javax.swing.JOptionPane;
import com.bullionxpress.shared.application.Application;
import com.bullionxpress.shared.service.AccountDatabase;
import com.bullionxpress.shared.service.AdminService;
import com.bullionxpress.shared.service.MarketService;
import com.bullionxpress.shared.service.ChangePasswordService;
import com.bullionxpress.shared.service.ChartService;
import com.bullionxpress.shared.service.ForgotPasswordService;
import com.bullionxpress.shared.service.InstrumentDatabase;
import com.bullionxpress.shared.service.LoginService;
import com.bullionxpress.shared.service.OrderService;
import com.bullionxpress.shared.service.PositionService;
import com.bullionxpress.shared.service.QuoteService;
import com.bullionxpress.shared.service.TransactionService;
import com.bullionxpress.shared.service.UserDatabase;
import com.bullionxpress.shared.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class Client
{
    public static String getProperty(String property) {
        try {
            Properties p = new Properties();
            p.load(new FileInputStream(new File("./client.properties")));
            return p.getProperty(property);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "<NA>";
    }
    public void connect()
    {
       Registry registry;
       String serverAddress=ClientGlobals.local?"localhost":"xagau.com";
       String serverPort="8888";
       try{
           // get the “registry”
           Log.info("Connecting to registry...");
           registry=LocateRegistry.getRegistry(
               serverAddress,
               Integer.parseInt(serverPort)
           );
           // look up the remote object
           Log.info("Binding interfaces...");

           ClientGlobals.loginService = (LoginService)(registry.lookup("LoginService"));
           Log.info("Bound LoginService");
           ClientGlobals.accountDatabase = (AccountDatabase)(registry.lookup("AccountDatabase"));
           Log.info("Bound AccountDatabase");
           ClientGlobals.instrumentDatabase = (InstrumentDatabase)(registry.lookup("InstrumentDatabase"));
           Log.info("Bound ChartService");
           ClientGlobals.orderService = (OrderService)(registry.lookup("OrderService"));
           Log.info("Bound InstrumentDatabase");
           ClientGlobals.chartService = (ChartService)(registry.lookup("ChartService"));
           Log.info("Bound OrderService");
           ClientGlobals.forgotPasswordService = (ForgotPasswordService)registry.lookup("ForgotPasswordService");
           Log.info("Bound ForgotPasswordService");
           ClientGlobals.changePasswordService = (ChangePasswordService)(registry.lookup("ChangePasswordService"));
           Log.info("Bound ChangePasswordService");
           ClientGlobals.positionService = (PositionService)(registry.lookup("PositionService"));
           Log.info("Bound PositionService");
           ClientGlobals.quoteService = (QuoteService)(registry.lookup("QuoteService"));
           Log.info("Bound QuoteService");
           ClientGlobals.transactionService = (TransactionService)(registry.lookup("TransactionService"));
           Log.info("Bound TransactionService");
           ClientGlobals.marketService = (MarketService)(registry.lookup("MarketService"));
           Log.info("Bound MarketService");
           ClientGlobals.userDatabase = (UserDatabase)(registry.lookup("UserDatabase"));
           Log.info("Bound UserDatabase");
           ClientGlobals.adminService = (AdminService)(registry.lookup("AdminService"));
           Log.info("Bound AdminService");

           ClientGlobals.cm = new ClientManager();
           Log.info("Bound all interfaces");
       }
       catch(RemoteException e){
           e.printStackTrace();
           JOptionPane.showMessageDialog(null, "Unable to connect to server.\nPlease try again later.\nApplication will now exit.");
           Application.quit();
       }
       catch(NotBoundException e){
           e.printStackTrace();
       }
    }
}

